/**
 * 企业管理,主要功能包括：
 *   1. 企业信息完善
 *   2. 企业查询
 * @author lms 2022.07.28
 */
package ms.core.gateway.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ms.core.common.ApiResult;
import ms.core.common.AppException;
import ms.core.common.Page;
import ms.core.common.RequestBody;
import ms.core.common.consts.SysErr;
import ms.core.common.utils.UploadedFileUtils;
import ms.core.common.utils.UploadedFileUtils.FileDoc;
import ms.core.gateway.bean.SysCorpBean;
import ms.core.gateway.request.CreateCorpRequest;
import ms.core.gateway.request.QueryCorpRequest;
import ms.core.gateway.request.UpdateCorpRequest;
import ms.core.gateway.response.SysCorpVO;
import ms.core.gateway.system.Application;
import ms.core.gateway.tool.SessionTool;
import ms.core.gateway.utils.SysCorpUtils;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;

public class SysCorpService {
	/**
	 * 接口：<div style="color:red"><b> newCorp </b></div><p>
	 * 功能：新增企业信息 <p>
	 * 参数: ctype=>企业类型(>=1根据实际应用做扩展) <p>
	 * 
	 * @param req
	 * @param resp
	 * @param body={CreateCorpRequest 对象}
	 * @param sid
	 * @return
	 */
	public ApiResult newCorp(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		CreateCorpRequest corp = CreateCorpRequest.getInstance(body);

		SysCorpUtils.newCorp(corp);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> edtCorp </b></div><p>
	 * 功能：更新企业信息 <p>
	 * 场景：1.管理员修改任意企业; 2.管理员或会员修改隶属企业信息(无需传企业uid参数)<p>
	 * 注意: 如果企业类型发生变更，企业下帐号类型需要变更，同时权限也需要重置。<p>
	 * 参数: ctype=>企业类型(2的n次幂,1-平台企业;>=1其他) <p>
	 * 
	 * @param req
	 * @param resp
	 * @param body={SysCorpDTO 对象}
	 * @param sid
	 * @return
	 */
	public ApiResult edtCorp(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		UpdateCorpRequest corp = UpdateCorpRequest.getInstance(body, sid);

		SysCorpUtils utils = new SysCorpUtils();
		utils.updateCorp(corp);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> delCorp </b></div><p>
	 * 功能：删除企业信息 <p>
	 * 场景：管理员删除企业信息
	 * 
	 * @param req
	 * @param resp
	 * @param body={"id":企业Id}
	 * @param sid
	 * @return
	 */
	public ApiResult delCorp(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		long id = JsonTool.getLongValue(body, "id", -1L);

		SysCorpBean corp = SysCorpUtils.delCorp(id);
		if (corp!=null && Application.corpDelegate!=null) {
			Application.corpDelegate.afterDeleteCorp(corp);
		}

		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> sttCorp </b></div><p>
	 * 功能：管理员设置企业状态 <p>
	 * 描述: 企业状态由正常->注销，企业下帐号都需要置为注销；注销改为正常，则不处理帐号状态
	 * 
	 * @param req
	 * @param resp
	 * @param body={"uid":"企业uid","state":状态(0-正常;1-注销)}
	 * @param sid
	 * @return
	 */
	public ApiResult setCorpState(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String uid = rb.getStrValue("uid", null);
		int state = rb.getIntValue("state", -1);
		if (StrTool.isBlank(uid) || state<0 || state>1)
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.setCorpState(uid, state);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> newCorpDocTyp </b></div><p>
	 * 功能：新建资料目录 <p>
	 *     
	 * @param req
	 * @param resp
	 * @param body={"corpUid":"企业uid","docType":"资料类别名"}
	 * @param sid
	 * @return
	 */
	public ApiResult newDocType(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String corpUid = rb.getStrValue("corpUid", null);
		String docType = rb.getStrValue("docType", null);
		if (StrTool.isBlank(corpUid) || StrTool.isBlank(docType)) 
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.newDocType(corpUid, docType.trim());
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> edtCorpDocTyp </b></div><p>
	 * 功能：修改资料目录 <p>
	 *     
	 * @param req
	 * @param resp
	 * @param body={"corpUid":"企业uid","oldDocType":"原资料类别名","oldDocType":"新资料类别名"}
	 * @param sid
	 * @return
	 */
	public ApiResult edtDocType(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String corpUid = rb.getStrValue("corpUid", null);
		String oDocType = rb.getStrValue("oldDocType", null);
		String nDocType = rb.getStrValue("newDocType", null);
		if (StrTool.isBlank(corpUid) || StrTool.isBlank(oDocType) || StrTool.isBlank(nDocType) || oDocType.equalsIgnoreCase(nDocType)) 
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.edtDocType(corpUid, oDocType, nDocType);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> delCorpDocTyp </b></div><p>
	 * 功能：删除资料目录 <p>
	 *     
	 * @param req
	 * @param resp
	 * @param body={"corpUid":"企业uid","docType":"资料类别名"}
	 * @param sid
	 * @return
	 */
	public ApiResult delDocType(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String corpUid = rb.getStrValue("corpUid", null);
		String docType = rb.getStrValue("docType", null);
		if (StrTool.isBlank(corpUid) || StrTool.isBlank(docType)) 
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.delDocType(corpUid, docType);
		return new ApiResult(true);
	}
	
	/**
	 * 接口：<div style="color:red"><b> uplCorpDoc </b></div><p>
	 * 功能：上传企业资料 <p>
	 * 前端传参: 采用 multipart/form-data 表单方式提交参数
	 *     参数: <key,value> 键值对方式
	 *          1. files 存储附件列表
	 *     		2. docType(n) 对应附件描述，如 docType1 对应附件1的资料类型
	 *     
	 * @param req
	 * @param resp
	 * @param body={"corpUid":"企业uid","docType1":"资料1类型","docType2":"资料2类型",...
	 * 				"files":[{"dst":"上传后磁盘文件1","src":"资料1源文件名","name":"doc1"},{"dst":"上传后磁盘文件2","src":"资料2源文件名","name":"doc2"},...]}
	 * @param sid
	 * @return
	 */
	public ApiResult uploadDoc(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String corpUid = rb.getStrValue("corpUid", null);
		List<FileDoc> list = UploadedFileUtils.toFileDocList("catalog", "docType", "doc", rb);
		if (StrTool.isBlank(corpUid) || list==null || list.size()==0) 
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.updateDoc(corpUid, list);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> edtCorpDoc </b></div><p>
	 * 功能：修改企业资料分类及说明 <p>
	 *     
	 * @param req
	 * @param resp
	 * @param body={"corpUid":"企业uid","file":"资料文件名","docType":"资料类型","catalog":"资料目录"}
	 * @param sid
	 * @return
	 */
	public ApiResult edtDoc(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String corpUid = rb.getStrValue("corpUid", null);
		String file    = rb.getStrValue("file", null);
		String docType = rb.getStrValue("docType", null);
		String catalog = rb.getStrValue("catalog", null);
		if (StrTool.isBlank(corpUid) || StrTool.isBlank(file)) 
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.editDoc(corpUid, file, catalog, docType);
		return new ApiResult(true);
	}
	
	/**
	 * 接口：<div style="color:red"><b> delCorpDoc </b></div><p>
	 * 功能：删除企业资料 <p>
	 *     
	 * @param req
	 * @param resp
	 * @param body={"corpUid":"企业uid","files":["file1","file2",...]}
	 * @param sid
	 * @return
	 */
	public ApiResult deleteDoc(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String corpUid = rb.getStrValue("corpUid", null);
		List<String> list = JsonTool.jsonToListBean(rb.get("files"), String.class);
		if (StrTool.isBlank(corpUid) || list==null || list.size()==0) 
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysCorpUtils utils = new SysCorpUtils();
		utils.deleteDoc(corpUid, list);
		return new ApiResult(true);
	}

	private void formatCorp(SysCorpVO corp) {
		if (corp==null) return;

		if (Application.userDelegate!=null) 
			Application.userDelegate.formatCorp(corp);
	}

	private void formatCorp(List<SysCorpVO> corps) {
		if (corps==null || corps.size()==0) 
			return;

		for(int i=0; i<corps.size(); i++) {
			formatCorp(corps.get(i));
		}
	}

	/**
	 * 接口：<div style="color:red"><b> dtlCorp </b></div><p>
	 * 功能：我的企业详细信息 <p>
	 * 
	 * @param req={"uid":"企业uid"}
	 * @param resp
	 * @param body
	 * @param sid
	 * @return
	 */
	public ApiResult getCorp(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		String corpId = JsonTool.getStrValue(body, "uid", null);
		if (StrTool.isBlank(corpId) || !SessionTool.isSuperCorp(sid)) {
			corpId = SessionTool.getCorpUid(sid);
		}

		SysCorpVO corp = SysCorpUtils.getCorp(corpId);
		formatCorp(corp);
		return new ApiResult(true).setData(corp);
	}

	/**
	 * 接口：<div style="color:red"><b> qryCorp </b></div><p>
	 * 功能：企业查询(管理员功能) <p>
	 * 
	 * @param req
	 * @param resp
	 * @param body={QueryCorpRequest 对象}
	 * @param sid
	 * @return
	 */
	public ApiResult qryCorp(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		QueryCorpRequest qry = JsonTool.objectToBean(body, QueryCorpRequest.class);
		if (qry==null)
			throw new AppException(SysErr.SYS_PARAM_ERR, "查询对象不可为空.");

		SysCorpUtils utils = new SysCorpUtils();
		Page<SysCorpVO> pg = utils.queryCorp(qry);
		formatCorp(pg.getData());

		return new ApiResult(true).setPage(pg).setData(pg.getData());
	}
}
